package com.audi.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.activity.SelectDevicesActivity;
import com.audi.adapter.SQLiteAsusNewHelper;
import com.audi.dto.DeviceItem;
import com.audi.dto.PlaceItem;
import com.audi.message.ActionMessage;
import com.audi.message.SendMessage;

public class BasePlaceDialog implements ActionMessage {
	private Activity mActivity;
	private SendMessage sendMessage;
	private PlaceItem placeItem = new PlaceItem();

	public BasePlaceDialog(Activity activity) {
		this.mActivity = activity;
		sendMessage = new SendMessage();
	}

	public void selectPrivacyPlace() {
		final AudiDialog selectStatusPlace = new AudiDialog(mActivity);
		selectStatusPlace.setLayout(R.layout.select_capability);
		selectStatusPlace.show();
		ListView listCapability = (ListView) selectStatusPlace
				.findViewById(R.id.list_privacy);
		String[] stringArray = mActivity.getResources().getStringArray(
				R.array.list_privacy_text);
		listCapability.setAdapter(new CustomListAdapter(mActivity,
				R.layout.row_privacy, stringArray));
		listCapability.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				showNewPlaceDialog(arg2, addPlace);
				selectStatusPlace.dismiss();
			}
		});
	}

	public void editNamePlace(final PlaceItem itemPlace, final Handler callBack) {
		if (itemPlace.getPlaceId() != 1) {
			final AudiDialog diaEditNameDialog = new AudiDialog(mActivity);
			diaEditNameDialog.setContentView(R.layout.rename_place_dialog);
			final EditText renameEditText = (EditText) diaEditNameDialog
					.findViewById(R.id.edt_rename_place_dilog_input);
			Button cancel = (Button) diaEditNameDialog
					.findViewById(R.id.btn_rename_place_dialog_skip);
			Button ok = (Button) diaEditNameDialog
					.findViewById(R.id.btn_rename_place_dialog_ok);
			renameEditText.setText(itemPlace.getPlaceName());
			renameEditText.setSelection(renameEditText.getText().length());
			cancel.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					diaEditNameDialog.dismiss();
				}
			});

			ok.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (renameEditText.getText().toString().length() > 0) {
						PlaceItem de = itemPlace;
						de.setPlaceName(renameEditText.getText().toString());
						sendMessage.renamePlace(de, callBack);
					} else {
						Toast.makeText(mActivity, "Input name place",
								Toast.LENGTH_LONG).show();
					}
					diaEditNameDialog.dismiss();
				}
			});

			diaEditNameDialog.show();
		} else
			Toast.makeText(mActivity, "This Room is Uncategorized",
					Toast.LENGTH_SHORT).show();
	}

	public void editNameDevice(final DeviceItem itemDevice,
			final Handler callBack) {

		final AudiDialog diaEditNameDialog = new AudiDialog(mActivity);
		diaEditNameDialog.setContentView(R.layout.rename_place_dialog);
		final EditText renameEditText = (EditText) diaEditNameDialog
				.findViewById(R.id.edt_rename_place_dilog_input);
		Button cancel = (Button) diaEditNameDialog
				.findViewById(R.id.btn_rename_place_dialog_skip);
		Button ok = (Button) diaEditNameDialog
				.findViewById(R.id.btn_rename_place_dialog_ok);
		renameEditText.setText(itemDevice.getDeviceName());
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				diaEditNameDialog.dismiss();
			}
		});

		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (renameEditText.getText().toString().length() > 0) {
					itemDevice.setDeviceName(renameEditText.getText()
							.toString());
					DeviceItem newItem = new DeviceItem();
					newItem.setDeviceId(itemDevice.getDeviceId());
					newItem.setDeviceName(itemDevice.getDeviceName());
					sendMessage.updateDevice(newItem, callBack);

				} else {
					Toast.makeText(mActivity, "Input name Device",
							Toast.LENGTH_LONG).show();
				}
				diaEditNameDialog.dismiss();
			}
		});

		diaEditNameDialog.show();
	}

	public void editNameDevice(String deviceid) {

		final AudiDialog diaEditName = new AudiDialog(mActivity);
		diaEditName.setContentView(R.layout.rename_device_dialog);
		// TextView cam = (TextView) diaEditName.findViewById(R.id.text_camera);
		final EditText renameEditText = (EditText) diaEditName
				.findViewById(R.id.edt_rename_device_dilog_input);

		Button cancel = (Button) diaEditName
				.findViewById(R.id.btn_rename_device_dialog_skip);
		Button ok = (Button) diaEditName
				.findViewById(R.id.btn_rename_device_dialog_ok);
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				diaEditName.dismiss();
			}
		});

		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// DBHelper dbHelper = new DBHelper();

				SQLiteAsusNewHelper mSQLHelper = new SQLiteAsusNewHelper(
						mActivity);
				// dbHelper.addDataTest(mSQLHelper);
				DeviceItem item = new DeviceItem();
				item.setDeviceId(6);
				item.setDeviceName(renameEditText.getText().toString());
				mSQLHelper.renameDevice(item);
				int w;
				w = mSQLHelper.renameDevice(item);
				if (w == 1) {
					diaEditName.dismiss();
				}

			}
		});

		diaEditName.show();
	}

	public void removeDevice(final DeviceItem itemDevice, final Handler callBack) {

		final AudiDialog diaRemove = new AudiDialog(mActivity);
		diaRemove.setContentView(R.layout.remove_card_dialog);
		// TextView cam = (TextView) diaEditName.findViewById(R.id.text_camera);

		Button cancel = (Button) diaRemove.findViewById(R.id.btn_remove_cancel);
		Button ok = (Button) diaRemove.findViewById(R.id.btn_remove_yes);
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				diaRemove.dismiss();
			}
		});
		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// DBHelper dbHelper = new DBHelper();
				DeviceItem newItem = new DeviceItem();
				newItem.setDeviceId(itemDevice.getDeviceId());
				newItem.setDeviceName(itemDevice.getDeviceName());
				sendMessage.updateDevice(newItem, callBack);

			}
		});

		diaRemove.show();
	}

	// 3-1-2-2 Select public/privateTap
	public void changeCapabilityOfPlace(final PlaceItem placeItem,
			final Handler callBack) {
		final AudiDialog selectStatusPlace = new AudiDialog(mActivity);
		selectStatusPlace.setLayout(R.layout.select_capability);
		selectStatusPlace.show();
		ListView listCapability = (ListView) selectStatusPlace
				.findViewById(R.id.list_privacy);
		String[] stringArray = mActivity.getResources().getStringArray(
				R.array.list_privacy_text);
		listCapability.setAdapter(new CustomListAdapter(mActivity,
				R.layout.row_privacy, stringArray));
		listCapability.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				placeItem.setCapabilityType(arg2 + 1);
				sendMessage.changeCapabilityPlace(placeItem, callBack);
				selectStatusPlace.dismiss();
			}
		});
	}

	private class CustomListAdapter extends ArrayAdapter<String> {
		String[] stringArray;

		public CustomListAdapter(Context context, int textViewResourceId,
				String[] objects) {
			super(context, textViewResourceId, objects);
			stringArray = objects;
		}

		@SuppressLint("ViewHolder")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = ((Activity) mActivity)
					.getLayoutInflater();
			View row = inflater.inflate(R.layout.row_privacy, parent, false);
			TextView dateText = (TextView) row.findViewById(R.id.text_privacy);
			dateText.setText(stringArray[position]);
			return row;
		}
	}

	public void removePlace(final PlaceItem placeItem, final Handler callBack) {
		if (placeItem.getPlaceId() != 1) {
			final AudiDialog dialog = new AudiDialog(mActivity);
			dialog.setContentView(R.layout.dialog_remove_place_card);
			TextView mNamePlaceRemove = (TextView) dialog
					.findViewById(R.id.title_remove_place);
			mNamePlaceRemove.setText(mActivity
					.getString(R.string.title_remove_place)
					+ " "
					+ placeItem.getPlaceName() + "?");
			dialog.setTitle("Remove this place");
			dialog.show();
			Button yes_btn_dialog = (Button) dialog
					.findViewById(R.id.id_yes_dialog);
			yes_btn_dialog.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					sendMessage.deletePlace(placeItem, callBack);
					dialog.dismiss();
				}
			});
			Button mBtnNo = (Button) dialog.findViewById(R.id.id_no_dialog);
			mBtnNo.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.cancel();
				}
			});
		} else
			Toast.makeText(mActivity, "This Room is Uncategorized",
					Toast.LENGTH_SHORT).show();
	}

	// 3-1-2-3 Create a new place
	public void showNewPlaceDialog(final int privacy, final Handler callBack) {
		final AudiDialog newPlaceDialog = new AudiDialog(mActivity);
		newPlaceDialog.setLayout(R.layout.new_device_dialog);
		newPlaceDialog.show();
		TextView createNewPlaceTitle = (TextView) newPlaceDialog
				.findViewById(R.id.txt_new_device_dilog_title);
		TextView createNewPlaceHint = (TextView) newPlaceDialog
				.findViewById(R.id.txt_new_device_dilog_hint);
		final EditText createNewPlaceEdit = (EditText) newPlaceDialog
				.findViewById(R.id.edit_new_device_dialog_input);
		Button cancleBtn = (Button) newPlaceDialog
				.findViewById(R.id.btn_new_device_dialog_skip);
		final Button okBtn = (Button) newPlaceDialog
				.findViewById(R.id.btn_new_device_dialog_next);
		createNewPlaceEdit.setHint(R.string.create_place_hint);
		createNewPlaceEdit.setText("");
		createNewPlaceTitle.setText(R.string.create_place_title);
		createNewPlaceHint.setVisibility(View.GONE);
		cancleBtn.setText(R.string.cancel);
		okBtn.setText(R.string.ok);
		okBtn.setClickable(false);
		okBtn.setAlpha(0.5f);
		createNewPlaceEdit.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if (createNewPlaceEdit.getText().toString().equals("")) {
					okBtn.setClickable(false);
					okBtn.setAlpha(0.5f);
					// Toast.makeText(context,"Loi",
					// Toast.LENGTH_SHORT).show();
				} else {
					okBtn.setAlpha(1);
					okBtn.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							final PlaceItem itemp = new PlaceItem();
							itemp.setPlaceImage("");
							itemp.setPlaceName(createNewPlaceEdit.getText()
									.toString());
							itemp.setCapabilityType(privacy);
							placeItem.setCapabilityType(privacy + 1);
							placeItem.setPlaceName(createNewPlaceEdit.getText()
									.toString());
							sendMessage.sendData(CHECK_PLACE_EXIST, itemp,
									callBack);
							newPlaceDialog.dismiss();
						}
					});

				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});

		cancleBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				newPlaceDialog.dismiss();
			}
		});
	}

	// 3-1-2-4 Notice Dialog
	public void showNoticePlace() {
		final AudiDialog noticeDialog = new AudiDialog(mActivity);
		noticeDialog.setLayout(R.layout.notice_new_place_dialog);
		noticeDialog.show();
		Button cancle = (Button) noticeDialog
				.findViewById(R.id.btn_notice_place_ok);
		cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				noticeDialog.dismiss();
			}
		});
	}

	/**
	 * addPlace
	 * 
	 * @see add Place
	 */
	@SuppressLint("HandlerLeak")
	private Handler addPlace = new Handler() {

		public void handleMessage(final Message msg) {
			mActivity.runOnUiThread(new Runnable() {
				public void run() {
					try {
						if (msg.arg1 != -1) {
							if (!msg.getData().getBoolean(
									String.valueOf(CHECK_PLACE_EXIST))) {
								Intent intent = new Intent(mActivity,
										SelectDevicesActivity.class);
								Bundle bundleInfoPlace = new Bundle();
								bundleInfoPlace.putSerializable("placeItem",
										placeItem);
								intent.putExtras(bundleInfoPlace);
								mActivity.startActivity(intent);
							} else {
								showNoticePlace();
							}
						}
					} catch (Exception e) {
					}
				}
			});
		}
	};
}
